//
//  SEGAdvertisementViewModel.m
//  SEGViewModel
//
//  Created by 山刘 on 2020/6/9.
//

#import "SEGAdvertisementNewViewModel.h"

@interface SEGAdvertisementNewViewModel()
@property (nonatomic, strong, readwrite) RACCommand *requestCommand;
@property (strong, nonatomic, readwrite) RACSubject *messageSubject;
@end
@implementation SEGAdvertisementNewViewModel
#pragma mark - Init
-(instancetype)init{
    if (self = [super init]) {
        [self racInit];
    }
    return self;
}

- (void)racInit {
    _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(NSNumber*  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            NSInteger position = [input integerValue];// 广告类型
            
            // 检查启动广告逻辑
            if (position == SEGAdvlistType_START) {
                
                [[SEGAdvlistManager sharedManager] segRequestAdvlistWithPositionType:position completed:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull codeType, NSString * _Nonnull errorMessage) {
                    
                    NSArray<SEGAdvertismentModel *> *startAdvertisData = result;
                    
                    // 启动广告类型逻辑
                    if (isSuccessed && startAdvertisData && startAdvertisData.count > 0) {
                        [subscriber sendNext:@{@"type":@(SEGAdvlistType_START),@"value":startAdvertisData.firstObject}];
                        [subscriber sendCompleted];
                        return;
                    }
                    
                    // 启动广告不存在的情况，再检查弹窗广告数据
                    [self checkPopWindowAdvertisment:SEGAdvlistType_SYJ completed:^(SEGAdvertismentModel *result) {
                        if (result) [subscriber sendNext:@{@"type":@(SEGAdvlistType_SYJ),@"value":result}];
                        [subscriber sendCompleted];
                    }];
                    
                }];
                
            }else{
                
                // 启动弹窗广告逻辑
                [self checkPopWindowAdvertisment:position completed:^(SEGAdvertismentModel *result) {
                    if (result) [subscriber sendNext:@{@"type":@(SEGAdvlistType_SYJ),@"value":result}];
                    [subscriber sendCompleted];
                }];
                
            }
            return nil;
        }];
    }];
}

-(void)dealloc{
    [self.requestCommand rac_deallocDisposable];
    [self.messageSubject rac_deallocDisposable];
}

#pragma mark - Publish Methods

/// 弹窗广告
/// 弹窗广告上线之后，每天打开APP最多出现一次，不论是退出APP还是后台运行APP:
/// 需求表里弹窗广告显示次数那个，每人每个小区最多一天显示一次，如果当天切换小区了，
/// 那就再显示一次，切换多少次显示多少次，如果切换回去就不显示了
+(BOOL)segCanUsePopWindowRecord:(SEGAdvlistType)position{
    if (position == SEGAdvlistType_SYJ && [[SEGAdvlistManager sharedManager] segGetCurrentMonthAndDayAndUserIdAndCommunityIDToUserDefaults]) {
        return YES;
    }
    return NO;
}

#pragma mark - Private Methods
/// 弹窗广告逻辑
-(void)checkPopWindowAdvertisment:(SEGAdvlistType)position completed:(void(^)(SEGAdvertismentModel* result))completed{
    
    if (![SEGAdvertisementNewViewModel segCanUsePopWindowRecord:position]) {
        completed(nil);
        return;
    }
    
    [[SEGAdvlistManager sharedManager] segRequestAdvlistWithPositionType:SEGAdvlistType_SYJ completed:^(BOOL isSuccessed, id  _Nonnull result, NSString * _Nonnull codeType, NSString * _Nonnull errorMessage) {
        NSArray<SEGAdvertismentModel*> *popWindowData  = result;
        if (isSuccessed && popWindowData && popWindowData.count > 0) {
            [[SEGAdvlistManager sharedManager] segSaveCurrentMonthAndDayAndUserIdAndCommunityIDToUserDefaults];
            completed(popWindowData.firstObject);
            return;
        }
        completed(nil);
    }];
}

#pragma mark - Getter
-(RACSubject *)messageSubject{
    if (!_messageSubject) {
        _messageSubject = [RACSubject subject];
    }
    return _messageSubject;
}
#pragma mark - Stter
@end
